package Hot100;

import java.util.HashSet;
import java.util.Set;

public class 最长连续序列 {
    public int longestConsecutive(int[] nums) {
        Set<Integer> set = new HashSet<>();
        for (int num : nums) set.add(num);
        int lenMax = 0;
        for (int num : set) {
            if (!set.contains(num - 1)) {
                int len = 0;
                while (set.contains(num++)) len++;
                lenMax = Math.max(lenMax, len);
            }
        }
        return lenMax;
    }
}
